package txc.test.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {
	private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ExceptionHandler(value = { Exception.class })
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public String defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		// e.printStackTrace();
		// ModelAndView mav = new ModelAndView();
		// mav.addObject("exception", e);
		// mav.addObject("url", req.getRequestURL());
		// mav.setViewName("error");
		logger.error("Exception occured url:{}, error message: {}", req.getRequestURI(), e.getMessage());
		e.printStackTrace();

		return "/error";
	}

	// @ExceptionHandler(value = RuntimeException.class)
	// @ResponseBody
	// public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req,
	// RuntimeException e) throws Exception {
	// ErrorInfo<String> r = new ErrorInfo<>();
	// e.printStackTrace();
	//
	// r.setMessage(e.getMessage());
	// r.setCode(ErrorInfo.ERROR);
	// r.setData("not found");
	// r.setUrl(req.getRequestURL().toString());
	// return r;
	// }
	//
	// @ExceptionHandler(SQLException.class)
	// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	// @ResponseBody
	// public ResultData handleSQLException(HttpServletRequest request,
	// Exception ex) {
	// return ResultData.result(false).setMsg(ex.getMessage());
	// }

}
